
package com.contactmodule.dao;
import com.contactmodule.bean.TbDepartUserMiddle;
import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbDepartUserMiddleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbDepartUserMiddle tbDepartUserMiddle=null;
    private List<TbDepartUserMiddle> tbDepartUserMiddleList=null;

    public boolean add(TbDepartUserMiddle inTbDepartUserMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_DepartUserMiddle(DepartId,UserId,IsMaster,CreateTime) values(?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbDepartUserMiddle.getDepartId());
        pt.setInt(2,inTbDepartUserMiddle.getUserId());
        pt.setInt(3,inTbDepartUserMiddle.getIsMaster());
        pt.setString(4,inTbDepartUserMiddle.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inDepartUserMiddleId){
        return UtilDAO.del("Tb_DepartUserMiddle", "DepartUserMiddleId", inDepartUserMiddleId);
    }
    public boolean edit(TbDepartUserMiddle inTbDepartUserMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_DepartUserMiddle set DepartUserMiddleId=?,DepartId=?,UserId=?,IsMaster=?,CreateTime=? where DepartUserMiddleId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbDepartUserMiddle.getDepartUserMiddleId());
        pt.setInt(2,inTbDepartUserMiddle.getDepartId());
        pt.setInt(3,inTbDepartUserMiddle.getUserId());
        pt.setInt(4,inTbDepartUserMiddle.getIsMaster());
        pt.setString(5,inTbDepartUserMiddle.getCreateTime());
        pt.setInt(6,inTbDepartUserMiddle.getDepartUserMiddleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbDepartUserMiddle getTbDepartUserMiddleByDepartUserMiddleId(int inDepartUserMiddleId) {
        tbDepartUserMiddle=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DepartUserMiddle where DepartUserMiddleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inDepartUserMiddleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbDepartUserMiddle == null)tbDepartUserMiddle = getTbDepartUserMiddle(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDepartUserMiddle;
    }
    public List<TbDepartUserMiddle> getTbDepartUserMiddleAll() {
        tbDepartUserMiddleList=new ArrayList<TbDepartUserMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DepartUserMiddle";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDepartUserMiddleList.add(getTbDepartUserMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDepartUserMiddleList;
    }
    public List<TbDepartUserMiddle> getTbDepartUserMiddleAll(VskPage vp) {
        tbDepartUserMiddleList=new ArrayList<TbDepartUserMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DepartUserMiddle";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_DepartUserMiddle", "DepartUserMiddleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDepartUserMiddleList.add(getTbDepartUserMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDepartUserMiddleList;
    }
    public TbDepartUserMiddle getTbDepartUserMiddle(ResultSet rs) throws Exception{
        TbDepartUserMiddle tbDepartUserMiddle = new TbDepartUserMiddle();
        tbDepartUserMiddle.setDepartUserMiddleId(rs.getInt("DepartUserMiddleId"));
        tbDepartUserMiddle.setDepartId(rs.getInt("DepartId"));
        tbDepartUserMiddle.setUserId(rs.getInt("UserId"));
        tbDepartUserMiddle.setIsMaster(rs.getInt("IsMaster"));
        tbDepartUserMiddle.setCreateTime(rs.getString("CreateTime"));
        return tbDepartUserMiddle;
    }
}
